package com.sell.converter;

import com.google.gson.reflect.TypeToken;
import com.sell.dataobject.OrderDetail;
import com.sell.dto.OrderDTO;
import com.sell.exceptions.ResultEnum;
import com.sell.exceptions.SellException;
import com.sell.from.OrderFrom;
import com.sell.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: megan
 * @eMail: meganmail@vip.qq.com
 * @date: 2018/12/6 16:01
 * @version: v1.0
 * @description: orderFrom --> orderDTO
 */
@Slf4j
public class OrderFrom2OrderDTOConverter {

    public static OrderDTO converter(OrderFrom orderFrom) {
        OrderDTO orderDTO = new OrderDTO();
        orderDTO.setBuyerPhone(orderFrom.getPhone());
        orderDTO.setBuyerName(orderFrom.getPhone());
        orderDTO.setBuyerOpenid(orderFrom.getOpenid());
        orderDTO.setBuyerAddress(orderFrom.getAddress());

        String item = orderFrom.getItem();
        Gson gson = new Gson();
        List<OrderDetail> orderDetailList;
        try {
            orderDetailList = gson.fromJson(item,
                    new TypeToken<List<OrderDetail>>() {
                    }.getType());
        } catch (Exception e) {
            log.error("【对象转换】错误, string={}", item);
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        log.debug("orderDetailList --> {}", orderDetailList);
        orderDTO.setOrderDetailList(orderDetailList);
        return orderDTO;
    }
}
